package com.bank.utils;

import com.mysql.cj.util.StringUtils;
import org.mockito.internal.util.StringUtil;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: Lixy
 * @email: 1908598362@qq.com
 * @date: 2022/1/25 星期二 23:01
 */
public class ValidatorUtils {

    private static final Pattern phone_pattern = Pattern.compile("[1]([3-9])[0-9]{9}$");


    public static boolean isPhone(String phone){
        if (StringUtils.isNullOrEmpty(phone)){
            return false;
        }
        Matcher matcher = phone_pattern.matcher(phone);
        return matcher.matches();
    }

    public static String GetCode() {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 6; i++) {
            int value = random.nextInt(10);
            sb.append(value);
        }
        String s = sb.toString();
        return s;
    }

    public static Integer getRemainSecondsDay() {
        Date currentDate = new Date();
        LocalDateTime midnight = LocalDateTime.ofInstant(currentDate.toInstant(),
                ZoneId.systemDefault()).plusDays(1).withHour(0).withMinute(0)
                .withSecond(0).withNano(0);
        LocalDateTime currentDateTime = LocalDateTime.ofInstant(currentDate.toInstant(),
                ZoneId.systemDefault());
        return (int) ChronoUnit.SECONDS.between(currentDateTime, midnight);
    }

    public static String GetFakeCode() {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 5; i++) {
            int value = random.nextInt(10);
            sb.append(value);
        }
        String s = sb.toString();
        return s;
    }

}
